﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NoDriverPrintHelperV1d0.Entity
{
    public class EscCommandV1d0
    {
        ArrayList Command = null;

        public EscCommandV1d0()
        {
            this.Command = new ArrayList();
        }

        public ArrayList getCommand()
        {
            return this.Command;
        }

        private void addArrayToCommand(byte[] array)
        {
            for (int i = 0; i < array.Length; i++)
            {
                this.Command.Add(array[i]);
            }
        }

        public void addSelectErrorCorrectionLevelForQRCode(byte n)
        {
            byte[] command = { 29, 40, 107, 3, 0, 49, 69, 3 };
            command[7] = n;
            addArrayToCommand(command);
        }

        public void addSelectSizeOfModuleForQRCode(byte n)
        {
            byte[] command = { 29, 40, 107, 3, 0, 49, 67, 3 };
            command[7] = n;
            addArrayToCommand(command);
        }

        public void addStoreQRCodeData(String content)
        {
            byte[] command = { 29, 40, 107, 0, 0, 49, 80, 48 };
            command[3] = ((byte)((content.Length + 3) % 256));
            command[4] = ((byte)((content.Length + 3) / 256));
            addArrayToCommand(command);
            addStrToCommand(content, content.Length);
        }

        private void addStrToCommand(String str, int length)
        {
            byte[] bs = null;
            if (!str.Equals(""))
            {
                try
                {
                    bs = System.Text.Encoding.GetEncoding("GB2312").GetBytes(str);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                if (length > bs.Length)
                {
                    length = bs.Length;
                }
                for (int i = 0; i < length; i++)
                {
                    this.Command.Add(bs[i]);
                }
            }
        }

        public void addPrintQRCode()
        {
            byte[] command = { 29, 40, 107, 3, 0, 49, 81, 48 };
            addArrayToCommand(command);
        }

        public static byte[] toPrimitive(Byte[] array)
        {
            if (array == null)
            {
                return null;
            }
            if (array.Length == 0)
            {
                return new byte[] { };
            }
            byte[] result = new byte[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                result[i] = array[i];
            }
            return result;
        }

    }
}
